using UnityEngine;
using System.Collections;

public class dbLogin : MonoBehaviour {
	
	public string usuario;
	public string senha;
	public GUIText texto;

	
	void OnGUI() {
		GUIStyle style = new GUIStyle();
		
		style.fontSize = 18;
		style.normal.textColor = Color.white;
		
		GUI.Label(new Rect(142, 128, 150, 35), "Usuario:", style);
		usuario = GUI.TextField(new Rect(230, 120, 150, 35),  usuario, 15);
		GUI.Label(new Rect(150, 178, 150, 35), "Senha:", style);
		senha   = GUI.TextField(new Rect(230, 170, 150, 35),  senha, 15);	
		
		if (GUI.Button (new Rect(230, 220,70,30),"Entrar"))
		{
			carregaLogin_();
		}
		
		GUI.Button (new Rect(310, 220, 70, 30),"Cancelar");
	}
	
	public void carregaLogin_()
	{		
		StartCoroutine(carregaLogin());
	}
	
	IEnumerator carregaLogin()
	{
		WWWForm form = new WWWForm();
		
		form.AddField("usuario", usuario);
		form.AddField("senha", senha);
				
		WWW webRequest = new WWW(constant_sistem.db_url + "login.php", form);
	
		yield return webRequest;
		
		if (webRequest.error !=null) {
			Debug.Log(webRequest.error);
		} else {
			if (webRequest.text != "" && webRequest.text != "0"){
				constant_sistem.idUsuari_Atual = webRequest.text;
				Application.LoadLevel(2);				
			} else {
				texto.text = "Usuario ou senha invalidos";
			}
		}
		
	}
}
